package com.we.scrm.web;

import com.we.scrm.common.enums.CustomerLevelEnum;
import com.we.scrm.common.enums.RebateTypeEnum;
import com.we.scrm.common.page.Pagination;
import com.we.scrm.common.util.JsonView;
import com.we.scrm.domain.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CustomerController extends  AbstractController{
    @RequestMapping("/customerPage")
    public ModelAndView customerPage() {
        return prepareModelAndView("customerPage");
    }
    //获取会员等级列表
    @RequestMapping("level/list")
    @ResponseBody
    public String levelList(){
        return JsonView.successJson(CustomerLevelEnum.toList());
    }

    //获取佣金等级列表
    @RequestMapping("rebate/list")
    @ResponseBody
    public String rebateList(){
        return JsonView.successJson(RebateTypeEnum.toList());
    }
    //获取客户列表 - 分页
    @PostMapping("/customer/list")
    @ResponseBody
    public String customerList(Customer queryEntity, Pagination<Customer> page) {
        page = customerService.queryPage(queryEntity, page);
        return JsonView.successJson(page);
    }
    @RequestMapping("customer/info")
    @ResponseBody
    public String customerInfo(Customer entity) {
        return JsonView.successJson(customerService.getById(entity.getId()));
    }
    @PostMapping("/customer/update")
    @ResponseBody
    public String customeUpdate(Customer entity){
        if(null == entity.getId()){
            return JsonView.failureJson("用户ID不能为空！");
        }
        //修改
        this.customerService.update(entity);
        return JsonView.successJson(entity.getId());
    }
    //获取对对应客户的标签列表
    @RequestMapping("customerTags/list")
    @ResponseBody
    public String customerTagsList(Customer entity){
        return JsonView.successJson(customerTagsService.getCustomerTags(entity.getId()));
    }
    @PostMapping("/customer/saveTags")
    @ResponseBody
    public String saveTags(@RequestParam(name="id") Long id, @RequestParam(name="tags[]") List<String> tags) {
        if(null != id) {
            customerTagsService.updateCustomerTags(id, tags);
        }
        return JsonView.successJson(id);
    }
}
